﻿using System;
using System.IO;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Web;
using System.Web.Security;
using System.Security.Cryptography;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Jungle.Code.DAL;


namespace Jungle.Prodamo.PaySystem
{
    public class Account
    {
        private string ConnectionString = ConfigurationManager.ConnectionStrings["dbConnectionUserBase"].ConnectionString;
        // Create Account for user
        public void CreateAccount(int userId)
        {
            using (SqlConnection connect = new SqlConnection(ConnectionString))
            {
                SqlCommand command = new SqlCommand("mdAccount_Create", connect);
                SqlParameter param_id_user = command.Parameters.Add("@id_user", SqlDbType.Int);

                command.CommandType = CommandType.StoredProcedure;
                param_id_user.Value = userId;

                command.Connection.Open();
                command.ExecuteNonQuery();
                command.Connection.Close();
            }
        }

        public int AccountId(int userId)
        {
            using (SqlConnection connect = new SqlConnection(ConnectionString))
            {
                SqlCommand command = new SqlCommand("mdAccount_GetByUser", connect);
                SqlParameter param_id_user = command.Parameters.Add("@id_user", SqlDbType.Int);
                SqlDataReader reader = null;

                command.CommandType = CommandType.StoredProcedure;
                param_id_user.Value = userId;

                command.Connection.Open();
                reader = command.ExecuteReader();
                if (reader.Read()) return Convert.ToInt32(reader.GetValue(0));
                else return 0;
                reader.Close();
                command.Connection.Close();
            }            
        }

        public int GetUserIdByAccountId(int accId)
        {
            using (SqlConnection connect = new SqlConnection(ConnectionString))
            {
                SqlCommand command = new SqlCommand("mdAccount_GetUserId", connect);
                SqlParameter param_accId = command.Parameters.Add("@id_acc", SqlDbType.Int);
                SqlDataReader reader = null;

                command.CommandType = CommandType.StoredProcedure;
                param_accId.Value = accId;

                command.Connection.Open();
                reader = command.ExecuteReader();
                if (reader.Read())
                {
                    return Convert.ToInt32(reader.GetValue(1));
                }
                else return -1;
                reader.Close();
                command.Connection.Close();
            }
        }

        // Get account`s balans by user
        public double GetUserBalans(int userId)
        {
            using (SqlConnection connect = new SqlConnection(ConnectionString))
            {
                SqlCommand command = new SqlCommand("mdAccount_GetByUser", connect);
                SqlParameter param_id_user = command.Parameters.Add("@id_user", SqlDbType.Int);
                SqlDataReader reader = null;

                command.CommandType = CommandType.StoredProcedure;
                param_id_user.Value = userId;

                command.Connection.Open();
                reader = command.ExecuteReader();
                if (reader.Read()) return Convert.ToDouble(reader.GetValue(2));
                else return 0;
                reader.Close();
                command.Connection.Close();
            }
        }

        // Tranzact system cash between users
        public void TranzactInCash(int who, int from, double sum)
        {
            if ((GetUserBalans(who) - sum) >= 0)
            {
                changeBalance(who, sum * (-1));
                changeBalance(from, sum);
                using (SqlConnection connect = new SqlConnection(ConnectionString))
                {
                    SqlCommand command = new SqlCommand("mdAccount_History_Add", connect);
                    SqlParameter param_who = command.Parameters.Add("@user_who", SqlDbType.Int);
                    SqlParameter param_from = command.Parameters.Add("@user_from", SqlDbType.Int);
                    SqlParameter param_sum = command.Parameters.Add("@summ", SqlDbType.Float);

                    command.CommandType = CommandType.StoredProcedure;
                    param_who.Value = who;
                    param_from.Value = from;
                    param_sum.Value = sum;
                    command.Connection.Open();
                    command.ExecuteNonQuery();
                    command.Connection.Close();
                }
            }

        }

        // Changr balance
        public void changeBalance(int userId, double sum)
        {
            using (SqlConnection connect = new SqlConnection(ConnectionString))
            {
                SqlCommand command = new SqlCommand("mdAccount_ChangeBalance", connect);
                SqlParameter param_userId = command.Parameters.Add("@userId", SqlDbType.Int);
                SqlParameter param_sum = command.Parameters.Add("@sum", SqlDbType.Float);

                command.CommandType = CommandType.StoredProcedure;
                param_userId.Value = userId;
                param_sum.Value = sum;
                command.Connection.Open();
                command.ExecuteNonQuery();
                command.Connection.Close();
            }
        }


    }
}
